
const { ccclass, requireComponent, menu, property } = cc._decorator;

/**
 *  cocos pageView增强
 */
@ccclass
@menu("ipha组件/pageView增强")
@requireComponent(cc.PageView)
export default class Ipha_PageViewBoost extends cc.Component {

    @property({ type: cc.Label, displayName: "页面文字提示" })
    private pageTipLable: cc.Label = null;

    @property({ type: cc.PageView, displayName: "页面视图 组件" })
    private pageView: cc.PageView = null;


    private nowPageIndex: number = null;

    start() {
        this.updatePageTipLable();
        this.updatePageScale();
    }

    addPgae(page: cc.Node) {
        this.pageView.addPage(page);
    }

    updatePageTipLable(index?: number) {
        if (!this.pageTipLable) return;
        if (index && typeof index == "number") {
            this.pageTipLable.string = `${index} / ${this.pageView.getPages().length}`;
        } else {
            this.pageTipLable.string = `${this.pageView.getCurrentPageIndex() + 1} / ${this.pageView.getPages().length}`;
        }
    }

    updatePageScale() {
        // let cIdnex = this.pageView.getCurrentPageIndex();
        // if (this.nowPageIndex == cIdnex) return;
        // this.nowPageIndex = cIdnex;
        // this.pageView.getPages().forEach((elem, idnex, arr) => {
        //     if (idnex == cIdnex) {
        //         elem.children[0].scale = 1.4;
        //     } else {
        //         elem.children[0].scale = 1;
        //     }
        // });
    }

    OnBtnPageToRight() {
        if (this.pageView.getCurrentPageIndex() == this.pageView.getPages().length - 1) return;
        this.pageView.scrollToPage(this.pageView.getCurrentPageIndex() + 1, 0.3);
    }

    OnBtnPageToLeft() {
        if (this.pageView.getCurrentPageIndex() == 0) return;
        this.pageView.scrollToPage(this.pageView.getCurrentPageIndex() - 1, 0.3);
    }


    update(dt) {
        if (this.pageView.isScrolling() || this.pageView.isAutoScrolling()) {
            this.updatePageScale();
        }
        this.updatePageTipLable();
    }

    onDestroy() {
        // this.pageView.content.destroyAllChildren();
    }

}

